package org.space.galaxyCabinet.dao.impl;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.space.galaxyCabinet.EmqxClientWrapper;
import org.space.galaxyCabinet.constant.ControlTypeConstants;
import org.space.galaxyCabinet.constant.EmqxConstants;
import org.space.galaxyCabinet.domain.FreightChannel;
import org.space.galaxyCabinet.dao.FreightChannelControlDao;
import org.space.galaxyCabinet.domain.MQTTData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class FreightChannelControlDaoImpl implements FreightChannelControlDao {

    @Autowired
    private EmqxClientWrapper emqxClientWrapper;

    @Override
    public boolean out(FreightChannel freightChannel) {
        try {
            String topic = EmqxConstants.DEVICE_ALL_FREIGHT_CHANNEL.replace("all", freightChannel.getDeviceNo());
            log.info("topic:{}", topic);
            log.info("mqttData:{}", JSON.toJSONString(freightChannel));
            emqxClientWrapper.publish(topic, JSON.toJSONString(freightChannel));
            return true;
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
    }

    @Override
    public boolean selfInspection(FreightChannel freightChannel) {
        try {
            String topic = EmqxConstants.DEVICE_ALL_FREIGHT_CHANNEL.replace("all", freightChannel.getDeviceNo());
            log.info("topic:{}", topic);
            log.info("data:{}", JSON.toJSONString(freightChannel));
            emqxClientWrapper.publish(topic, JSON.toJSONString(freightChannel));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
